﻿Imports System.Data.SqlClient

Imports System.Text.RegularExpressions


Public Class FormCustomerEditor


    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click

        Dim pattern As String = "^0[0-9]{2} [0-9]{3} [0-9]{3,4}$"
        If Regex.IsMatch(Me.txtPhone.Text, pattern) = False Then
            MsgBox("Please input correct phone number!")
            Me.txtPhone.Focus()
            Exit Sub
        End If

        If Validator.IsPhone(Me.txtPhone.Text) = False Then
            MsgBox("Please input correct phone number!")
            Me.txtPhone.Focus()
            Exit Sub
        End If

        Cmd.Parameters("@CustomerName") = Me.txtCustomerName.Text
        Cmd.Parameters("@Phone") = Me.txtPhone.Text
        Cmd.Parameters("@Address") = Me.txtAddress.Text
        If Me.txtCustomerId.Text = "" Then
            Cmd.ExecuteNonQuery("INSERT INTO Customer VALUES(@CustomerName,@Phone,@Address,1);")
        Else
            Cmd.Parameters("@CustomerId") = txtCustomerId.Text
            Cmd.ExecuteNonQuery("UPDATE Customer SET CustomerName=@CustomerName,Phone=@Phone,Address=@Address WHERE CustomerId=@CustomerId")
        End If

        Me.DialogResult = Windows.Forms.DialogResult.OK

    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click

        Me.DialogResult = Windows.Forms.DialogResult.Cancel

    End Sub

    Private Sub FormCustomerEditor_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub
End Class